<?php
class catAction extends apiAction
{
	public $_mod = '';
	public $cache = '';
	public $cache_name = '';
    public function _initialize() {
		parent::_initialize();
		$this->_mod = D('daka_class_cat');
    }
    
	public function index() {
		$time = $this->_get('time','intval',strtotime(date('Y-m-d',time()-60*24*3600)));//默认前60天 当天整点时间戳 开始的数据
		$where = 'p.class_id <> 1';//取出不属于新人课堂
		$sql = "
select cat_id,class_id,id,title,img,url,share_num,read_num,uname,is_hot,ordid,class_img,class_name,cat_name from
(
    select *,if(@pa=ff.class_id,@rank:=@rank+1,@rank:=1) as ordid,@pa:=ff.class_id
    FROM
                (select class_id,title,p.time,cat_id,uname,url,share_num,read_num,is_hot,p.img,p.id,c.content as class_img,c.cname as class_name,cat.name as cat_name
                            from nks_daka_paper p
                            join nks_daka_class c on c.id = p.class_id
                            join nks_daka_class_cat cat on c.cat_id = cat.id
                            join nks_daka_user u on p.uid = u.id
                            where $where and p.time > $time and p.status = 1 and c.status = 1 and cat.status = 1
                            order by p.class_id asc,p.ordid asc,p.time desc) ff,
                (select @rank:=0,@pa:=null) tt
) result having ordid <=3;
		";
		$list = $this->_mod->query($sql);
		//dump($this->_mod);
		$data['msg'] = count($list)?'获取成功':'没有数据';
		$data['status'] = count($list)?1:0;
		$data['data'] = $list;
		$this->APIreturn($data);
    }
	public function detail($id) {
		$list = $this->cache;
		//找出属于id 的值
		foreach($list as $k => $v){
			if($v['id'] == $id){
				$json = $v;
			}
		}
		$data['msg'] = '获取成功';
		$data['status'] = count($json);
		$data['data'] = $json;
		$this->APIreturn($data);
    }
	public function cache() {
		if (false === $this->cache = F($this->cache_name)) {
			D('daka_class_cat')->set_cache();
			$this->cache = F($this->cache_name);
		}
    }
	public function get_user_data(&$class_list_data,$uid){
		$re = array();
		if($class_list_data){
			foreach($class_list_data as $k => $v){
				if($v['uid'] == $uid){
					$re[] = $v;
					unset($class_list_data[$k]);
				}
			}
		}
		return $re;
	}
	

   

}